import { Controller, Inject, Query, Get, Del, Body, Post, Put } from '@midwayjs/core';
import { UserService } from '../../service/user.service';
import { NUser } from '../../entity/user.entity';
import { R } from './vo/Result';

@Controller('/user')
export class UserController {

    @Inject()
    userService: UserService;

    @Get('/id')
    async findUserById(@Query('id') id: number): Promise<R<NUser>> {
      let user = await this.userService.findUserById(id);
      return new R(0, null, user);
    }
    @Get('/name')
    async findUserByName(@Query('name') name: string): Promise<R<NUser>> {
      let user = await this.userService.findUserByName(name);
      return new R(0, null, user);
    }
    @Del('/id')
    async delUserById(@Query('id') id: number) {
      let delResult = await this.userService.delUserById(id);
      return new R(0, null, delResult.raw);
    }

    @Post('/')
    async addUserById(@Body('data') data: NUser) {
      let user = await this.userService.addUser(data);
      return new R(0, null, user);
    }
    @Put('/')
    async updateUser(@Body('data') data: NUser) {
      let user = await this.userService.updateUser(data)
      return new R(0, null, user);
    }
}